import os
import json
# import demjson
import re
import getpass
import shutil



def get_dir_info(dir_path):
	dish_names = []
	for f in os.listdir(dir_path):
		if os.path.isdir(f):
			dish_names.append(f)
			# os.chdir(os.path.join(dir_path, f))
			# print(os.getcwd())
	# print(dish_names)
	for index, dish_name in enumerate(dish_names):
		# 重命名文件夹名字
		old_dir_name = os.path.join(dir_path, dish_name)
		new_dir_name = os.path.join(dir_path, "dish"+str(index))
		os.rename(old_dir_name, new_dir_name)
		os.chdir(new_dir_name)
		with open('dish_info.txt', 'r') as f:
			json_text = f.read()
			json_text = re.sub('\'','\"',json_text)
			json_info = json.loads(json_text)
			json_info["name"] = dish_name
			# print(str(json_info))
			with open('new_dish_info.txt', 'w', encoding='utf-8') as nf:
				nf.write(str(json_info))
			print("写入成功！")


COPY_URL = r"D:\food"

def copy_dir(dir_path):
	new_dir_path = os.path.join(COPY_URL, os.path.basename(dir_path)+'shop')
	shutil.copytree(dir_path, new_dir_path)
	for root, dirs, files in os.walk(new_dir_path):
		for name in files:
			if name.endswith('.txt'):
				os.remove(os.path.join(root, name))


if __name__ == "__main__":
	root_dir = os.getcwd()
	get_dir_info(os.getcwd())
	copy_dir(root_dir)